/*
** gcd.c
** 
** Made by (seakage2h)
** Login   <seakage2h@seakage2h-ubuntu>
** 
** Started on  Wed Mar 11 13:32:45 2009 seakage2h
** Last update Sun May 12 01:17:25 2002 Speed Blue
*/

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
    int a, b, r;
    char i = 0, j = 0, t = 0;                  /* powers of 2 */
    
    if (argc < 3 || argc > 3){
        perror("You should input two integers to get the GCD!!\n");
        exit(1);
    }

    a = atoi(argv[1]);
    b = atoi(argv[2]);

    if (a == 0)
        if (b == 0){
            perror("Hey! No two 0s can be involved in GCD!!\n");
            exit(2);
        }
        else {
            printf("The GCD is %d\n", b);
            exit(0);
        }
    else if (b == 0){
        printf("The GCD is %d\n", a);
        exit(0);
    }

    /* NOT THE MOST EFFICIENT */
/*     while(r = a % b){ */
/*         a = b; */
/*         b = r; */
/*     } */
    if (a == b){
        printf("The GCD is %d\n", a);
        exit(0);
    }
    else if (a < b){
        r = a;
        a = b;
        b = r;
    }
    /* make sure a > b */
    



/* unsigned int gcd(int u, int v) */
/*  { */
/*      int shift; */
 
/*      /\* Let u and v be absolute values *\/ */
/*      if (u < 0) u = -u; */
/*      if (v < 0) v = -v; */
 
/*      /\* GCD(0,x) := x *\/ */
/*      if (u == 0 || v == 0) */
/*        return u | v; */
 
/*      /\* Let shift := lg K, where K is the greatest power of 2 */
/*         dividing both u and v. *\/ */
/*      for (shift = 0; ((u | v) & 1) == 0; ++shift) { */
/*          u >>= 1; */
/*          v >>= 1; */
/*      } */
 
/*      while ((u & 1) == 0) */
/*        u >>= 1; */
 
/*      /\* From here on, u is always odd. *\/ */
/*      do { */
/*          while ((v & 1) == 0)  /\* Loop X *\/ */
/*            v >>= 1; */
 
/*          /\* Now u and v are both odd, so diff(u, v) is even. */
/*             Let u = min(u, v), v = diff(u, v)/2. *\/ */
/*          if (u < v) { */
/*              v -= u; */
/*          } else { */
/*              unsigned int diff = u - v; */
/*              u = v; */
/*              v = diff; */
/*          } */
/*          v >>= 1; */
/*      } while (v != 0); */
 
/*      return u << shift; */
/*  } */

    
    printf("The GCD is %d\n", b);
    
    return 0;
}

